﻿using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using BrnMall.WeiXin.Sdk.Domain;
using Core.Sdk.WeiXin.Tool;
using BrnMall.Sdk.WeiXin;

namespace BrnMall.WeiXin.Sdk.Mp
{
    public class MpMessageUtil
    {
        /// <summary>
        /// 预约成功通知
        /// </summary>
        /// <param name="openId">接收人</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendAddAppointMsg(string openId, TemplateData data, TemplateMiniProgram app, string url)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.TemplateId_AddOrder,
                data = data,
                url = url,
                miniprogram = app,
            };
            return Send(model);
        }
        /// <summary>
        /// 新订单通知
        /// </summary>
        /// <param name="openId">接收人</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendAddOrderMsg(string openId, TemplateData data, TemplateMiniProgram app, string url)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.TemplateId_AddOrder,
                data = data,
                url = url,
                miniprogram = app,
            };
            return Send(model);
        }
        /// <summary>
        /// 资金到账通知
        /// </summary>
        /// <param name="openId">接收人</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendPayMsg(string openId, TemplateData data, TemplateMiniProgram app, string url)
        {

            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.Template_Balance,
                data=data,
                url = url,
                miniprogram = app,
            };

            return Send(model);
        }

        /// <summary>
        /// 收货通知
        /// </summary>
        /// <param name="openId">接收人</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendRecieveOrderMsg(string openId, TemplateData data, TemplateMiniProgram app, string url)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.TemplateId_Finish,
                data = data,
                url = url,
                miniprogram = app,
            };
            return Send(model);
        }

        /// <summary>
        /// 骑手接单通知
        /// </summary>
        /// <param name="openId">接收人</param>
        /// <returns></returns>
        public static string SendRiderMsg(string openId, TemplateData data)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.Template_Delivery,
                data = data
            };
            var accessToken = get_access_token();
            return MpSdk.SendTemplateMessage(model, accessToken);
        }

        /// <summary>
        /// 订单异常提醒
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendOrderErrorMsg(string openId, TemplateData data)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.Template_Order_Tips,
                data = data
            };
            return Send(model);
        }

        /// <summary>
        /// 账户异常提醒
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SendAccountErrorMsg(string openId, TemplateData data)
        {
            var model = new TemplateMessage()
            {
                touser = openId,
                template_id = Config.Template_Account_Tips,
                data = new
                {
                    first = data.first,
                    DateTime = data.keyword1,
                    Location = data.keyword2,
                    EventDesc = data.keyword3,
                    remark = data.remark
                }
            };
            return Send(model);
        }

        private static string Send(TemplateMessage model)
        {
            var accessToken = get_access_token();
            var json = MpSdk.SendTemplateMessage(model, accessToken);
            if (!json.Contains(":0,"))
            {
                Log(string.Format("发送失败:{0},参数：{1}", json, JsonConvert.SerializeObject(model)));
            }
            return json;
        }

        public static string get_access_token()
        {
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", Config.appid, Config.secret);
            string html = RestHttp.Get(url);
            Regex r1 = new Regex("\"access_token\":\"([\\s\\S].*?)\"");
            return r1.Match(html).Groups[1].Value;
        }
        private static void Log(string msg)
        {
            LogUtil.WriteLog("push", msg);
        }
    }
}